package com.xudow.app.newui;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.ImageView;

import com.activeshare.edu.ucenter.models.user.UserProfileWithOther;
import com.xudow.app.Config;
import com.xudow.app.R;
import com.xudow.app.newui.base.BaseUiActivity;
import com.xudow.app.newui.zutils.ZPreferenceUtils;
import com.xudow.app.ui.task.UserSecretUpDataTask;

import java.util.HashMap;

import butterknife.BindView;

/**
 * 作者：zhouXJ on 2016/4/7 10:01
 * 隐私设置
 */
public class PrivacyActivity extends BaseUiActivity implements View.OnClickListener {

    @BindView(R.id.privacy_nickname_img)
    ImageView privacy_nickname_img;

    @BindView(R.id.privacy_true_name_img)
    ImageView privacy_true_name_img;

    @BindView(R.id.privacy_account_num_img)
    ImageView privacy_account_num_img;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_privacy);
    }

    public void onBackBtnClick(View view) {
        finish();
    }

    @Override
    protected void initData() {
        initBtnState();

    }

    @Override
    protected void initListener() {
        privacy_nickname_img.setOnClickListener(this);
        privacy_true_name_img.setOnClickListener(this);
        privacy_account_num_img.setOnClickListener(this);
    }

    private boolean isNickNameChecked = false;
    private boolean isTrueNameChecked =false;
    private boolean isAccountNumChecked = false;
    private String getStatu(boolean b){
        if (b)
            return "1";
        else
            return "0";
    }
    @Override
    public void onClick(View view) {
        initBtnState();
        String url="";
        String key="status";
        String pramer="";
        switch (view.getId()) {
            case R.id.privacy_nickname_img: // 通过昵称找到我
                /*isNickNameChecked=!isNickNameChecked;
                if (isNickNameChecked) {  // 开
                    privacy_nickname_img.setImageResource(R.mipmap.btn_switch_p);
                } else { // 关
                    privacy_nickname_img.setImageResource(R.mipmap.btn_switch_c);
                }*/
                url= Config.FIND_BY_NICKNAME;
                pramer=getStatu(!isNickNameChecked);
                //ZPreferenceUtils.saveSwitchBtnState(this, ZPreferenceUtils.PREF_NICKNAME, isNickNameChecked);
                tag=ZPreferenceUtils.PREF_NICKNAME;
                value=isNickNameChecked;
                swtch=privacy_nickname_img;
                break;

            case R.id.privacy_true_name_img: // 通过真实姓名找到我
               /* isTrueNameChecked=!isTrueNameChecked;
                if (isTrueNameChecked) { // 开
                    privacy_true_name_img.setImageResource(R.mipmap.btn_switch_p);
                } else { // 关
                    privacy_true_name_img.setImageResource(R.mipmap.btn_switch_c);
                }*/
                url=Config.FIND_BY_REALNAME;
                pramer=getStatu(!isTrueNameChecked);
                //ZPreferenceUtils.saveSwitchBtnState(this, ZPreferenceUtils.PREF_TRUE_NAME, isTrueNameChecked);
                tag=ZPreferenceUtils.PREF_TRUE_NAME;
                value=isTrueNameChecked;
                swtch=privacy_true_name_img;
                break;

            case R.id.privacy_account_num_img: //  通过账号找到我
                /*isAccountNumChecked=!isAccountNumChecked;
                if (isAccountNumChecked) { // 开
                    privacy_account_num_img.setImageResource(R.mipmap.btn_switch_p);
                } else { // 关
                    privacy_account_num_img.setImageResource(R.mipmap.btn_switch_c);
                }*/
                url=Config.FIND_BY_USERCORD;
                pramer=getStatu(!isAccountNumChecked);
                //ZPreferenceUtils.saveSwitchBtnState(this, ZPreferenceUtils.PREF_ACCOUNT_NUM, isAccountNumChecked);
                tag=ZPreferenceUtils.PREF_ACCOUNT_NUM;
                value=isAccountNumChecked;
                swtch=privacy_account_num_img;
                break;

        }
        HashMap<String,String> map=new HashMap<String, String>();
        map.put("url",url);
        map.put("key", key);
        map.put("pramer", pramer);
        UserSecretUpDataTask upDataTask=new UserSecretUpDataTask(PrivacyActivity.this,upDataHandler);
        addTask(upDataTask);
        upDataTask.execute(map);

    }
    /**
     *开关handler
     */
    private Handler upDataHandler=new Handler(){
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            if (msg.what==UserSecretUpDataTask.SUCCESS){
                getMyApp().makeToast("开关成功");
                saveEdit(tag, value, swtch);
            }else{
                String str=msg.getData().getString("errMessage");
                if (null==str)
                    str="开关不成功";
                getMyApp().makeToast(str);
            }

        }
    };

    String tag;
    boolean value;
    ImageView swtch;

    private void saveEdit(String spTag,boolean value,ImageView swtch){
        value=!value;
        if (value){//开
           swtch.setImageResource(R.mipmap.btn_switch_p);
    } else { // 关
            swtch.setImageResource(R.mipmap.btn_switch_c);
        }
        ZPreferenceUtils.saveSwitchBtnState(this, spTag, value);
    }
    /**
     * 初始化按钮是否选中状态
     */
    private void initBtnState() {
        isNickNameChecked = ZPreferenceUtils.getSwitchBtnState(this, ZPreferenceUtils.PREF_NICKNAME);
        if (isNickNameChecked) {
            privacy_nickname_img.setImageResource(R.mipmap.btn_switch_p);
        } else {
            privacy_nickname_img.setImageResource(R.mipmap.btn_switch_c);
        }

        isTrueNameChecked = ZPreferenceUtils.getSwitchBtnState(this, ZPreferenceUtils.PREF_TRUE_NAME);
        if (isTrueNameChecked) {
            privacy_true_name_img.setImageResource(R.mipmap.btn_switch_p);
        } else {
            privacy_true_name_img.setImageResource(R.mipmap.btn_switch_c);
        }

        isAccountNumChecked = ZPreferenceUtils.getSwitchBtnState(this, ZPreferenceUtils.PREF_ACCOUNT_NUM);
        if (isAccountNumChecked) {
            privacy_account_num_img.setImageResource(R.mipmap.btn_switch_p);
        } else {
            privacy_account_num_img.setImageResource(R.mipmap.btn_switch_c);
        }
    }
}
